#ifdef WITH_OPENGL
#include "texture_opengl.h"
#include "canvas.h"
#include "opengl.h"
#include <cassert>
using namespace krryn::view;
using namespace krryn::system_imp;

#define Q { GLenum e = glGetError(); assert(e == GL_NO_ERROR); }

texture_opengl::texture_opengl(canvas *a_Canvas, size_t a_TextureUnit) : texture(a_Canvas, a_TextureUnit){
	glEnable(GL_TEXTURE_2D);
	Q;
	glGenTextures(1, &m_TextureId);
	Q;
	glBindTexture(GL_TEXTURE_2D, m_TextureId);
	Q;

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	Q;
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	Q;

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_WrapU == texture_repeat ? GL_REPEAT : GL_CLAMP);
//	Q;
//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_WrapV == texture_repeat ? GL_REPEAT : GL_CLAMP);
//	Q;

	// load texture into opengl
	glTexImage2D(
		GL_TEXTURE_2D, 
		0, 
		GL_RGBA, 
		m_Canvas->get_width(),
		m_Canvas->get_height(), 
		0,
		GL_RGBA,
		GL_UNSIGNED_BYTE,
		(GLubyte*)m_Canvas->get_buffer()
	);

	Q;
}

void texture_opengl::enable(){
	glBindTexture(GL_TEXTURE_2D, m_TextureId);

	//glActiveTexture(GL_TEXTURE0 + (GLenum)m_TextureUnit);
	
	//glClientActiveTexture(GL_TEXTURE0 + (GLenum)m_TextureUnit);
}

void texture_opengl::disable(){
}
#endif